package com.ucar.training;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author XiongMM
 * @date 2019/7/26 10:06
 * @description
 */
public class FirstServlet extends HttpServlet{

    @Override
    public void init() throws ServletException {
        System.out.println("我是FirstServlet,我是最先被调用的方法,用于初始化一些东西");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<html><body>您好，恭喜完成第一个servlet!!!<br/>");
        String value1 = this.getServletConfig().getInitParameter("key1");
        out.print("<p>key1的值为："+value1+"</p><br/>");
        String value2 = this.getServletConfig().getInitParameter("key2");
        out.print("<p>key2的值为："+value2+"</p><br/>");

        String value3 = this.getServletContext().getInitParameter("context-key1");

        out.print("<h3>context-key1的值为："+value3+"</h3><br/>");

        Object value4 = this.getServletContext().getAttribute("context-key2");
        out.print("<h3>context-key2的值为："+String.valueOf(value4)+"</h3><br/>");

        out.print("</body></html>");


    }

    @Override
    public void destroy() {
        System.out.println("我是FirstServlet,我是最后执行的");
    }
}
